package com.cattsoft.spring.aop.j2se.kettas.j2se;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class LoggingProxy implements InvocationHandler {
	private Object proxyobj;
	
	public LoggingProxy(Object obj) {
		proxyobj = obj;
	}
	
	public static Object bind(Object obj){
		Class cls = obj.getClass();
		return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new LoggingProxy(obj));
	}
	public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
		beforeAdvice(method);
		Object object =  method.invoke(proxyobj, args);
		afterAdvice(method);
		return object;
	}

	private void afterAdvice(Method method) {
		 System.out.println("after calling mehtod::"+method.getName());
	}

	private void beforeAdvice(Method method) {
		System.out.println("before calling mehtod::"+method.getName());
	}
	
}
